package com.student.liu.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.student.liu.entity.Orderform;
import com.student.liu.service.IOrderformDetailService;
import com.student.liu.service.IOrderformService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Map;

/**
 * 订单相关 接口
 */
@Slf4j
@RestController
@RequestMapping("detail")
public class OrderformDetailController extends BaseController {

    @Resource
    IOrderformDetailService orderformDetailService;

    /**
     * 创建订单
     */
    @PostMapping("create")
    public Object create(@RequestBody CreateOrderformParam createOrderformParam) {
        orderformDetailService.createOrderformDetail(createOrderformParam.getGoodsId(),
                createOrderformParam.getOrderformId(), createOrderformParam.count);
        return success("订单创建成功", null);
    }

    @PostMapping("orderpage")
    public Object page(@Valid @RequestBody OrderformDetailController.OrderPageBody orderPageBody, BindingResult result) throws InterruptedException {
        IPage<Map<String, Object>> goodsMaps = orderformDetailService.orderDetailPage(orderPageBody.getPageIndex(), 10,orderPageBody.getOrderformId());
        return success("", goodsMaps);
    }

    /**
     * 厨师任务查询
     * @return
     * @throws InterruptedException
     */
    @PostMapping("task")
    public Object task() throws InterruptedException {
        IPage<Map<String, Object>> goodsMaps = orderformDetailService.task();
        return success("", goodsMaps);
    }

    /**
     * 服务员任务查询
     * @return
     * @throws InterruptedException
     */
    @PostMapping("waitertask")
    public Object waitertask() throws InterruptedException {
        IPage<Map<String, Object>> goodsMaps = orderformDetailService.waiterTask();
        return success("", goodsMaps);
    }

    /**
     * 支付完成
     * @return
     */
    @GetMapping("payMoney/{id}")
    public Object putOn(@PathVariable Integer id) {
        orderformDetailService.update(id, (byte) 2);
        return success("订单支付完成", null);
    }

    /**
     * 订单副本取消
     * @param orderformId
     * @return
     */
    @GetMapping("cancel/{orderformId}")
    public Object cancel(@PathVariable Integer orderformId) {
        orderformDetailService.update(orderformId, (byte) 5);
        return success("取消订单", null);
    }

    /**
     * 菜品订单副本制作完成
     * @param orderformId
     * @return
     */
    @GetMapping("cookOver/{orderformId}")
    public Object cookOver(@PathVariable Integer orderformId) {
        orderformDetailService.cookUpdateState(orderformId, (byte) 3);
        return success("制作完成", null);
    }

    /**
     * 菜品送达
     * @param orderformId
     * @return
     */
    @GetMapping("Over/{orderformId}")
    public Object Over(@PathVariable Integer orderformId) {
        orderformDetailService.waiterupdate(orderformId, (byte) 4);
        return success("菜品已送达", null);
    }

    /**
     * 删除订单副本
     * @param orderform
     * @return
     */
    @PostMapping("remove")
    public Object remove(@RequestBody Orderform orderform) {
        orderformDetailService.remove(orderform.getId());
        return success("删除未支付订单成功", null);
    }

    @Data
    static class CreateOrderformParam{
        Integer goodsId;    //商品ID
        Integer orderformId;    //商品规格ID
        Short count;      //购买数量
    }

    @Data
    static class OrderPageBody {
        @NotNull(message = "请传递页码")
        private Integer pageIndex;
        @NotNull(message = "请传递生成总订单编号")
        private Integer orderformId;
    }
}
